class GoGame:
    def __init__(self, size=9):
        self.board_size = size
        self.board = [['＋' for _ in range(size)] for _ in range(size)]
        self.current_player = '○'  # 黑棋先行
        self.directions = [(-1,0), (1,0), (0,-1), (0,1)]

    def print_board(self):
        # 打印列坐标
        print(' 　' + '　'.join([chr(ord('A') + i) for i in range(self.board_size)]))
        # 打印棋盘
        for i in range(self.board_size):
            print(f"{i+1:2}", end=' ')
            print(' '.join(self.board[i]))
        print()
    def has_liberty(self, x, y):
        color = self.board[x][y]
        if color == '＋': return False  # 空点无气
        visited = set()
        queue = [(x, y)]
        visited.add((x, y))
        while queue:
            cx, cy = queue.pop(0)
            for dx, dy in self.directions:
                nx, ny = cx + dx, cy + dy
                if 0 <= nx < self.board_size and 0 <= ny < self.board_size:
                    if self.board[nx][ny] == '＋':
                        return True  # 存在气
                    elif self.board[nx][ny] == color and (nx, ny) not in visited:
                        visited.add((nx, ny))
                        queue.append((nx, ny))
        return False  # 无气
    def remove_group(self, x, y):
        color = self.board[x][y]
        queue = [(x, y)]
        removed = []
        while queue:
            cx, cy = queue.pop(0)
            if self.board[cx][cy] == color:
                self.board[cx][cy] = '＋'
                removed.append((cx, cy))
                for dx, dy in self.directions:
                    nx, ny = cx + dx, cy + dy
                    if 0 <= nx < self.board_size and 0 <= ny < self.board_size:
                        queue.append((nx, ny))
        return removed
    def is_valid_move(self, row, col):
        # 简单验证：位置是否在棋盘内且为空
        return (0 <= row < self.board_size and 
                0 <= col < self.board_size and
                self.board[row][col] == '＋')

    def play(self):
        print("围棋游戏开始！(输入坐标如'A1'或'Q'退出)")
        while True:
            self.print_board()
            print(f"玩家 {self.current_player} 的回合")
            
            # 获取输入
            move = input("请输入坐标：").upper()
            if move == 'Q':
                print("游戏结束")
                break

            # 解析坐标
            try:
                col = ord(move[0:1]) - ord('A')
                row = int(move[1:]) - 1
            except (ValueError, IndexError):
                print("无效输入，请使用字母+数字格式（如A1）")
                continue

            # 验证落子
            if self.is_valid_move(row, col):
                # 临时落子
                self.board[row][col] = self.current_player
                opponent = '●' if self.current_player == '○' else '○'
                removed = []
                # 检查周围敌方棋块是否被提
                for dx, dy in self.directions:
                    nx, ny = row + dx, col + dy
                    if 0 <= nx < self.board_size and 0 <= ny < self.board_size:
                        if self.board[nx][ny] == opponent and not self.has_liberty(nx, ny):
                            removed += self.remove_group(nx, ny)
                # 检查自杀规则：己方棋块是否无气
                if not self.has_liberty(row, col):
                    self.board[row][col] = '＋'  # 撤销落子
                    print("禁着点，不能落子")
                else:
                    self.board[row][col] = self.current_player
                    # 切换玩家（实际围棋规则没有轮流限制，这里简化）
                    self.current_player = '●' if self.current_player == '○' else '○'
            else:
                print("无效落子位置")

if __name__ == "__main__":
    game = GoGame(size=13)
    game.play()
